package com.xpfirst.socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import com.xpfirst.ARMServerActivity;
import com.xpfirst.ActivityManage;

import android.util.Log;

public class SocketServer implements Runnable{
	public int PORT;
	public ServerSocket serverSocket = null;
	public void closeListen(){
		if((serverSocket != null) && (serverSocket.isBound())){
			try{
				serverSocket.close();
				serverSocket = null;
			}catch(IOException e){
				Log.v("server socket close IOException", e.getMessage());
			}
		}
	}
	public void run() {
		try {
            ActivityManage.sendMsg(2, "host", "socket is listen");
			serverSocket=new ServerSocket(PORT);
			while(true)
			{
				Log.v("socket", "isListen");
				Socket socket = serverSocket.accept();
				socket.setKeepAlive(true);
//				socket.setSoTimeout(60);   //设置超时 
//		          // tcp no delay transfer,default false
//	            socket.setTcpNoDelay(true);
//	            
//	            // 中断后未传输数据可传输的时间（秒）,defalut false
//	            socket.setSoLinger(true, 240);
//	           
//	            // 设置超时时间(毫秒)
//	            socket.setSoTimeout(180000);
//	           
//	            // 设置接收的缓冲区大小
//	            socket.setReceiveBufferSize(100);
//	           
//	            // 设置发送的缓冲区大小
//	            socket.setSendBufferSize(200);
				SocketManage.creatSocketClient(socket);
			}
		} catch (IOException e) {
			Log.v("server socket IOException", e.getMessage());
			serverSocket = null;
		}
	}
}
